<?php

/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */

namespace app\ticket\controller;

use app\common\controller\Ucenter;


class User extends Ucenter {
	protected $noNeedLogin = [];
	protected $noNeedRight = ['*'];
	protected $steps;
	protected $currend_step;
	protected $custom_nav;
	protected $admin_info;
	protected $staff_info;
	protected $user_info;
	protected $ticketModel;
	protected $ticketStaffModel;
	protected $ticketStatusModel;
	protected $ticketRoleModel;
	protected $ticketMessageModel;
	protected $ticketCategoryModel;
	protected $userModel;

	protected function initialize()
	{
		parent::initialize();
		$this->ticketModel         = model('ticket/Ticket');
		$this->ticketStaffModel    = model('ticket/TicketStaff');
		$this->ticketStatusModel   = model('ticket/TicketStatus');
		$this->ticketMessageModel  = model('ticket/TicketMessage');
		$this->ticketCategoryModel = model('ticket/TicketCategory');
		$this->userModel           = model('common/user');
		$this->app_id = 100001030;
		$this->assign_sub_menu($this->app_id);
        //流程步骤
		$this->assign_steps();
		$this->set_step(1);

        //个人首页初始化
		$this->ticketNav();
//		$this->getUser();
		$this->setItem();
		$this->assign('meta_title', '工单系统');
		$this->assign('site_name',  ' 工单系统');
	}

	public function index()
	{
        $this->initUser();
        $this->initStaff();
        $this->initAdmin();

        return $this->fetch('user/index');
	}

	public function set_step($current = 1)
	{
		$this->currend_step = $current;
		$this->assign('current_step', $this->currend_step);
	}

	public function assign_steps()
	{
		$this->instal_steps = [
			'1' => ['id' => 1, 'title' => '选择问题所属产品'],
			'2' => ['id' => 2, 'title' => '选择问题类型'],
			'3' => ['id' => 3, 'title' => '推荐解决方案'],
			'4' => ['id' => 4, 'title' => '创建工单'],
		];

		$this->assign('install_steps', $this->instal_steps);

	}

    //顶部导航
	private function ticketNav()
	{
		//显示不同的顶部导航

		switch($this->user_info['group_id']) {

			case '11':
				$this->custom_nav = [
					[
						'url'   => url('ticket/index/index'),
						'title' => '首页',
						'class' => '',
					],
					[
						'url'   => url('ticket/manage.staff/index'),
						'title' => '员工管理',
						'class' => '',
						'child' => [
							[
								'url'   => url('ticket/manage.staff/index'),
								'title' => '员工管理',
								'class' => '',
							], [
								'url'   => url('ticket/manage.staff/edit'),
								'title' => '新增员工',
								'class' => '',
							],
						],
					],
					[
						'url'   => url('ticket/manage.ticket/allot'),
						'title' => '分配工单',
						'class' => '',
					],
					[
						'url'   => url('ticket/manage.product/index'),
						'title' => '产品管理',
						'class' => '',
						'child' => [
							[
								'url'   => url('ticket/manage.product/index'),
								'title' => '产品管理',
								'class' => '',
							], [
								'url'   => url('ticket/manage.product/edit'),
								'title' => '新增产品',
								'class' => '',
							],
						],
					],
					[
						'url'   => url('ticket/manage.category/index'),
						'title' => '问题分类管理',
						'class' => '',
						'child' => [
							[
								'url'   => url('ticket/manage.category/index'),
								'title' => '分类管理',
								'class' => '',
							],
							[
								'url'   => url('ticket/manage.category/edit'),
								'title' => '添加分类',
								'class' => '',
							],
						],
					],
					[
						'url'   => url('ticket/manage.solution/index'),
						'title' => '解决方案管理',
						'class' => '',
						'child' => [
							[
								'url'   => url('ticket/manage.solution/index'),
								'title' => '解决方案管理',
								'class' => '',
							], [
								'url'   => url('ticket/manage.solution/edit'),
								'title' => '解决方案新增',
								'class' => '',
							],
						],
					],
					[
						'url'   => url('ticket/user/index'),
						'title' => '个人中心',
						'class' => '',
					],
				];
				break;
			case '21':
				$this->custom_nav = [
					[
						'url'   => url('ticket/index/index'),
						'title' => '首页',
						'class' => '',
					],
					[
						'url'   => url('ticket/manage.message/index'),
						'title' => '消息',
						'class' => '',
					],
					[
						'url'   => url('ticket/manage.ticket/index'),
						'title' => '工单管理',
						'class' => '',
						'child' => [
							[
								'url'   => url('ticket/manage.ticket/wait'),
								'title' => '带处理工单',
								'class' => '',
							], [
								'url'   => url('ticket/manage.ticket/dispose'),
								'title' => '处理中工单',
								'class' => '',
							], [
								'url'   => url('ticket/manage.ticket/finish'),
								'title' => '已处理工单',
								'class' => '',
							],[
								'url'   => url('ticket/manage.ticket/makeover'),
								'title' => '已转让工单',
								'class' => '',
							],
						],
					],
					[
						'url'   => url('ticket/user/index'),
						'title' => '个人中心',
						'class' => '',
					],
				];
				break;
			default:
				$this->custom_nav = [
					[
						'url'   => url('ticket/index/index'),
						'title' => '首页',
						'class' => '',
					],
					[
						'url'   => url('ticket/manage.ticket/index'),
						'title' => '历史工单',
						'class' => '',
					],
					[
						'url'   => url('ticket/user/index'),
						'title' => '个人中心',
						'class' => '',
					],
				];
				break;
		}

		//定制各角色独立导航菜单
		if( ! empty($this->custom_nav)) {
			$this->assign('menu_list', $this->custom_nav);
		}
	}

//    private function getUser()
//    {
//        $this->user = model('forum/forumUser')->where(['user_id' => $this->auth->id])->find();
//        if(empty($this->user)) {
//            $this->user          = new ForumUser();
//            $this->user->user_id = $this->user_info['id'];
//            $this->user->title   = $this->user_info['nickname'];
//            $this->user->status  = 1;
//            $this->user->save();
//        }
//    }

    //统计项
    private function setItem()
    {
        switch($this->user_info['group_id']) {
            case '11':
                $this->admin_info = model('common/user')->where(['id'=>$this->auth->id])->find();

//                $money_count       = model('gift/giftOrder')->where(['buy_uid' => $this->auth->id])->sum('money');

                $this->count_items = [
                    ['title' => '员工人数', 'value' => 20, 'url'=>url('ticket/manage.staff.index')],
                    ['title' => '带分配工单', 'value' => 30, 'url'=>url('ticket/manage.ticket.wait')],
                    ['title' => '已分配工单', 'value' => 40, 'url'=>url('ticket/manage.ticlet/finish')],
                    ['title' => '解决方案', 'value' => 50, 'url'=>url('ticket.manage.solution/index')],
                ];
                break;
            case '21':
                $this->staff_info = model('ticket/TicketStaff')->where(['uid'=>$this->auth->id])->find();

//                $money_count       = model('gift/giftOrder')->where(['buy_uid' => $this->auth->id])->sum('money');

                $this->count_items = [
                    ['title' => '待处理工单', 'value' => 20, 'url' => url('ticket/manage.ticket/wait')],
                    ['title' => '已处理工单', 'value' => 15,'url' => url('ticket/manage.ticket/finish')],
                    ['title' => '转让工单', 'value' => 10,'url' => url('ticket/manage.ticket/makeover')],
                ];
                break;
            case '31':
                $this->user_info = model('common/user')->where(['id'=>$this->auth->id])->find();
                $money_count       = model('gift/giftOrder')->where(['buy_uid' => $this->auth->id])->sum('money');

                $this->count_items = [
                    ['title' => '待提交工单', 'value' => 5, 'url' => url('ticket/manage.ticket/index')],
                    ['title' => '处理中工单', 'value' => 10,'url' => url('ticket/manage.message/index')],
                    ['title' => '历史工单', 'value' => 30,'url' => url('ticket/manage.ticket/historylist')],
                ];
                break;
        }

        $this->assign_sub_menu($this->app_id);
    }

    //初始化动态消息及通知
    public function initUser()
    {
        $this->assign('user_info', $this->user_info);
        $this->assign('auth_id', $this->user_info['group_id']);

        if( ! empty($this->user_info && $this->user_info['group_id'] == 3)) {
//            $game_order_list = model('play/playOrder')->where(['user_id' => $this->auth->id,'game_id'=>['gt',0]])->order('id desc')->limit(8)->select();
//            foreach ($game_order_list as &$item)
//            {
//                $item['status_name'] = $this->user_status_list[$item['status']];
//            }
//            $this->assign('game_order_list', $game_order_list);
        }
    }

    public function initStaff()
    {
        $this->assign('staff_info', $this->staff_info);
        $this->assign('auth_id', $this->user_info['group_id']);

        if( ! empty($this->user_info)) {

        }
    }

    public function initAdmin()
    {
        $this->assign('admin_info', $this->admin_info);
        $this->assign('auth_id', $this->user_info['group_id']);

        if( ! empty($this->user_info  && $this->user_info['group_id'] == 1)) {

        }
    }

}